package com.jzlin.alltools.core.http;

import com.jzlin.alltools.utils.LocaleUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * 基础服务返回体.
 *
 * @author linjz
 * @date 2024/9/14
 */
@Data
@ApiModel("基础服务返回体")
public class RestRet<T> {
    @ApiModelProperty("响应码，0为成功，错误一般为-1，具体看接口约定")
    int code;
    @ApiModelProperty("提示信息")
    String msg;
    @ApiModelProperty("返回数据，具体看接口约定")
    T data;

    public RestRet() {
    }

    public RestRet(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public RestRet(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> RestRet<T> createSuccessRet() {
        RestRet<T> ret = new RestRet<>();
        ret.setMsg(LocaleUtils.getMessage("msg.requestSuccess"));
        ret.setCode(0);
        return ret;
    }

    public static <T> RestRet<T> createSuccessRet(int code) {
        RestRet<T> ret = new RestRet<>();
        ret.setCode(code);
        return ret;
    }

    public static <T> RestRet<T> createSuccessRet(T data) {
        RestRet<T> ret = new RestRet<>();
        ret.setMsg(LocaleUtils.getMessage("msg.requestSuccess"));
        ret.setCode(0);
        ret.setData(data);
        return ret;
    }

    public static <T> RestRet<T> createSuccessRet(int code, String msg) {
        RestRet<T> ret = new RestRet<>();
        ret.setMsg(msg);
        ret.setCode(code);
        return ret;
    }

    public static <T> RestRet<T> createFailureRet() {
        RestRet<T> ret = new RestRet<>();
        ret.setMsg(LocaleUtils.getMessage("msg.requestFail"));
        ret.setCode(-1);
        return ret;
    }

    public static <T> RestRet<T> createFailureRet(int code, String msg) {
        RestRet<T> ret = new RestRet<>();
        ret.setMsg(msg);
        ret.setCode(code);
        return ret;
    }

    public static <T> RestRet<T> createFailureRet(String msg, T data) {
        RestRet<T> ret = new RestRet<>();
        ret.setMsg(msg);
        ret.setCode(-1);
        ret.setData(data);
        return ret;
    }

    public static <T> RestRet<T> createFailureRet(int code, String msg, T data) {
        RestRet<T> ret = new RestRet<>();
        ret.setCode(code);
        ret.setMsg(msg);
        ret.setData(data);
        return ret;
    }

    public static <T> RestRet<T> createFailureRet(String msg) {
        RestRet<T> ret = new RestRet<>();
        ret.setMsg(msg);
        ret.setCode(-1);
        return ret;
    }

    public static <T> RestRet<T> createFailureRet(T data) {
        RestRet<T> ret = new RestRet<>();
        ret.setMsg(LocaleUtils.getMessage("msg.requestFail"));
        ret.setCode(-1);
        ret.setData(data);
        return ret;
    }
}
